package com.quanshu.shop.goods.view;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.config.Config;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.MyApplication;
import com.quanshu.exchange.ui.gui.login.LoginActivity;
import com.quanshu.shop.base.tool.BaseTool;
import com.quanshu.shop.base.view.BaseTitleBar;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoodsWebviewActivity extends BaseActivity {

    @BindView(R.id.goods_webview)
    WebView goodsWebview;
    @BindView(R.id.goods_webview_title)
    BaseTitleBar goodsWebviewTitle;
    private String url;
    public static final String GOODS_WEBVIEW_URL = "goods_webview_url";
    private static final String TAG = "GoodsWebviewActivity";
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_webview);
        mContext = this;
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    private void initView() {
        ImmersionBar.with(this).statusBarDarkFont(true).statusBarColor("#ffffff").titleBar(goodsWebviewTitle).init();
        goodsWebviewTitle.setLeftImage(R.drawable.icon_back_black_2_0).setCenterText("活动专区").setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });
        url = getIntent().getExtras().getString(GOODS_WEBVIEW_URL);
        WebSettings webSettings = goodsWebview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        webSettings.setSupportZoom(false);
        goodsWebview.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                String[] ss = s.split("/");
                Log.d(TAG, "shouldOverrideUrlLoading: " + ss[ss.length - 1]);
                if (MyApplication.getInstance().getCurrentUser() == null) {
                    BaseTool.goActivity(mContext, LoginActivity.class, null);
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("goods_info_id", ss[ss.length - 1]);
                    BaseTool.goActivity(mContext, GoodsInfoActivity.class, bundle);
                }
                return true;
            }

            @Override
            public void onPageFinished(WebView webView, String s) {
                Log.d("加载完成", "onPageFinished: ");
                //goodsWebview.loadUrl("javascript:function setTop(){$('.top').hide();}setTop();");
                goodsWebview.setVisibility(View.VISIBLE);
                super.onPageFinished(webView, s);
            }
        });
        goodsWebview.loadUrl(Config.URL_HOST_SHOP_IMG + url);
    }
}
